package model;

/**
 * This class creates the login. It checks with the database to make sure the
 * user is valid and the password is valid.
 * 
 * @author Daranhdara Hun
 * @version 1.0
 */

public class Login {

	private String username = "";

	private String password = "";

	private String role = "";

	/**
	 * This constructor creates the the username and password.
	 * 
	 * @param username
	 *            This parameter is the name that is going to be referenced and
	 *            taken in as a string.
	 * @param password
	 *            This parameter is taken in as a string and used to be refenced
	 *            with the database.
	 */
	public Login(String username, String password, String role) {
		this.username = username;
		this.password = password;
		this.role = role;
	}

	/*
	 * public static void main (String[] args) throws Exception { MyQuery query
	 * = new MyQuery(); ArrayList<User> newUser =
	 * query.searchUserByName("John"); User user = newUser.get(0);
	 * user.setPassword("upsword"); System.out.println(user.getPassword());
	 * query.updateUser(user, user.getSSN()); }
	 */
	/**
	 * Checks to to see if the user has entered in the correct login information
	 * from the login screen LOGIN GUI must only allow passage if user if user
	 * is returned. If a null is return, GUI must ask for user to re-enter
	 * password.
	 * 
	 * @return user
	 */
	public User authenticate() {
		final MyQuery query = new MyQuery();
		User user;
		try 
		{
			user = query.authenticateUser(username, password);
			if (user == null)
			{
				return null;
			}
			else
			{
				System.out.println("Login Passed");
				user = query.authenticateRole(user, role);
				if(user != null)
				{
					return user;
				}
				return null;
			}
		}

		catch (Exception e) 
		{
			e.printStackTrace();
			System.out.println("Exception Error at");
			return null;
		}

	}

	/**
	 * null
	 * 
	 * @return null
	 */
	public String toString(){
		return null;
	}
}